// pages/goodDetails/index.js
import {getGoodDetail,getService,addToCart} from '../../apis/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsImages: [], //轮播图
    detailInfo: {},
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 500,
    services: [], //商品提供服务内容
    showBuyNow: false,
    type: '',
    mode: 'cart',
    goodsNum: 1,
    goodsId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('options---', options)
    this.setData({
      goodsId: options.goodsId
    })
    this.initData({
      data: {
        goodsId: options.goodsId
      }
    })
  },
  async initData(params){
    const  res = await getGoodDetail(params)
    const detail = res.data.detail
    console.log("detail----", detail)
    this.setData({
      goodsImages: detail.goods_images,
      detailInfo: detail
    })
     // 获取商品服务
     console.log('goodsId===', params)
     const servicesData = await getService(params)

    this.setData({
      services: servicesData.data.list
    })
  },
  // 立即购买
  buyNow(){
    this.setData({
      showBuyNow: true,
      mode: 'buyNow'
    })
  },
  async showCartItem(){
    this.setData({
      showBuyNow: true,
      mode: 'cart'
    })
   
  },
  async addToCart(){
 console.log('detailInfo', this.data.detailInfo)
    const detailInfo = this.data.detailInfo
    let data = {
      goodsId: detailInfo.goods_id,
      goodsSkuId: "0",
      goodsNum: this.data.goodsNum
    }
    const res = await addToCart({data:data})
    console.log("加入购物车的结果", res)
    if (res.status === 200) {
       this.setData({
         showBuyNow: false
       })
    }
  },
  onClose(){
    this.setData({
      showBuyNow: false
    })
  },
  changeGoodsNum(ev){
    console.log('商品个数', ev.detail)
    this.setData({
      goodsNum: ev.detail || 1
    })
  },
  // 跳转到购物车
  gotoCart(){
    wx.switchTab({
      url: '/pages/cart/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})